<!DOCTYPE HTML>
<html>
<head>
  <script type="text/javascript" src="../extension/third_party/closure/base.js"></script>
  <script type="text/javascript" src="../extension/utils.js"></script>
  <script type="text/javascript" src="../extension/secure_prober.js"></script>
  <script type="text/javascript" src="../extension/background.js"></script>
  <script type="text/javascript">
  goog.require('goog.testing.jsunit');
  goog.require('secureSites.Main');
  goog.require('secureSites.SecureProber');
  </script>
  <script type="text/javascript">
    function testRedirectsHttpGoogleRequestToHttpsGoogle() {
      AssumingExists.assertRedirectUrlOfRequestIs('https://www.google.com', 'http://www.google.com');
    }
    
    function testRedirectsHttpTwitterRequestToHttpsTwitter() {
      AssumingExists.assertRedirectUrlOfRequestIs('https://www.twitter.com', 'http://www.twitter.com');
    }
    
    function testLeavesHttpContainingQuerystringInTact() {
      AssumingExists.assertRedirectUrlOfRequestIs('https://www.twitter.com/?q=http://', 'http://www.twitter.com/?q=http://');
    }
    
    function testRedirectsUpperCaseHttp() {
      AssumingExists.assertRedirectUrlOfRequestIs('https://www.twitter.com', 'HTTP://www.twitter.com');
    }
    
    function testDoesNotRedirectSiteWithNoSecureVersion() {
      AssumingDoesNotExist.assertRedirectUrlOfRequestIs('http://www.twitter.com', 'http://www.twitter.com');
    }
    
    var Helpers = (function() {
      return {
        assertRedirectUrlOfRequestIs: function(expectedUrl, requestUrl) {
          var main = this.newSecureSites();
          var instruction = main.onBeforeRequestFilter(main, {url: expectedUrl});
          if (instruction.redirectUrl === undefined) {
            instruction.redirectUrl = requestUrl;
          }
          assertEquals(expectedUrl, instruction.redirectUrl);
        }
      }
    })();
    
    AssumingExists = (function() {
      return {
        assertRedirectUrlOfRequestIs: function(expectedUrl, requestUrl) { Helpers.assertRedirectUrlOfRequestIs.call(this, expectedUrl, requestUrl); },
        newSecureSites: function() {
          var alwaysTrueSecureProber = new secureSites.SecureProber();
          alwaysTrueSecureProber.secureVersionExistsFor = function(url) {
            return true;
          };
          return new secureSites.Main(alwaysTrueSecureProber);
        }
      };
    })();
    
    AssumingDoesNotExist = (function() {
      return {
        assertRedirectUrlOfRequestIs: function(expectedUrl, requestUrl) { Helpers.assertRedirectUrlOfRequestIs.call(this, expectedUrl, requestUrl); },
        newSecureSites: function() {
          var alwaysFalseSecureProber = new secureSites.SecureProber();
          alwaysFalseSecureProber.secureVersionExistsFor = function(url) {
            return false;
          };
          return new secureSites.Main(alwaysFalseSecureProber);
        }
      };
    })();
  </script>
</head>
</html>